package com.anrongtec.zcpt.weather;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;

import com.anrongtec.zcpt.R;
import com.anrongtec.zcpt.base.MBaseFragment;

import java.util.ArrayList;

public class WeatherActivity extends FragmentActivity {
    public static Typeface typeface;

    public static Typeface getTypeface(Context context) {
        return typeface;
    }

    private DynamicWeatherView weatherView;
    private MxxViewPager viewPager;

    public MxxViewPager getViewPager() {
        return viewPager;
    }
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TimingLogger logger = new TimingLogger("WeatherActivity.onCreate");
        if(typeface == null){
            typeface = Typeface.createFromAsset(getAssets(), "fonts/mxx_font2.ttf");
        }
        logger.addSplit("Typeface.createFromAsset");
        setContentView(R.layout.activity_weather);
        logger.addSplit("setContentView");
        viewPager = (MxxViewPager) findViewById(R.id.main_viewpager);
        if (Build.VERSION.SDK_INT >= 19) {
            viewPager.setPadding(0, UiUtil.getStatusBarHeight(), 0, 0);
        }
        AlphaAnimation alphaAnimation = new AlphaAnimation(0f, 1f);
        alphaAnimation.setDuration(260);
        alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                getWindow().setBackgroundDrawable(
                        new ColorDrawable(Color.BLACK));
            }
            @Override
            public void onAnimationRepeat(Animation animation) {}
            @Override
            public void onAnimationEnd(Animation animation) {}
        });
        viewPager.setAnimation(alphaAnimation);
        logger.addSplit("findViewPager");
        weatherView = (DynamicWeatherView) findViewById(R.id.main_dynamicweatherview);
        logger.addSplit("findWeatherView");
        loadAreaToViewPager();
        logger.addSplit("loadAreaToViewPager");
        logger.dumpToLog();

    }

    public void loadAreaToViewPager(){
        final ArrayList<ApiManager.Area> selectedAreas = ApiManager.loadSelectedArea(this);
        final MBaseFragment[] fragments = new MBaseFragment[selectedAreas.size() + 1] ;
        for(int i = 0; i<selectedAreas.size();i++){
            final ApiManager.Area area = selectedAreas.get(i);
            fragments[i+1] = WeatherFragment.makeInstance(area, ApiManager.loadWeather(this, area.id));
        }
        fragments[0] = SettingsFragment.makeInstance(selectedAreas);
        viewPager.setAdapter(new SimpleFragmentPagerAdapter(getSupportFragmentManager(), fragments));
        viewPager.setOnPageChangeListener(new MxxViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                weatherView.setDrawerType(((SimpleFragmentPagerAdapter) viewPager.getAdapter()).getItem(
                        position).getDrawerType());
            }
        });
        viewPager.setCurrentItem(1, false);
    }

    public void updateCurDrawerType() {
        weatherView.setDrawerType(((SimpleFragmentPagerAdapter) viewPager.getAdapter()).getItem(
                viewPager.getCurrentItem()).getDrawerType());
    }

    @Override
    protected void onResume() {
        super.onResume();
        weatherView.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        weatherView.onPause();
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        weatherView.onDestroy();
    }

    public static class SimpleFragmentPagerAdapter extends MxxFragmentPagerAdapter {

        private MBaseFragment[] fragments;

        public SimpleFragmentPagerAdapter(FragmentManager fragmentManager, MBaseFragment[] fragments) {
            super(fragmentManager);
            this.fragments = fragments;
        }


        @Override
        public MBaseFragment getItem(int position) {
            MBaseFragment fragment = fragments[position];
            fragment.setRetainInstance(true);
            return fragment;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return fragments[position].getTitle();
        }

        @Override
        public int getCount() {
            return fragments.length;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(((Fragment) object).getView());
            super.destroyItem(container, position, object);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
